package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import pojo.Book;
import util.DBUtil;
import util.FileProcessDataUtil;
import dao.BookDao;

public class BookDaoImpl implements BookDao {

	public int addBook(Book b) {
		String sql = "insert into book(id,name,publish_date,author,price,is_new,press) values(seq03.nextval,?,?,?,?,?,?)";
		Connection con=null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			con=DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, b.getName());
			pst.setDate(2, new java.sql.Date(b.getPublishDate().getTime()));
			pst.setString(3, b.getAuthor());
			pst.setDouble(4, b.getPrice());
			pst.setBoolean(5, b.isNew());
			pst.setString(6, b.getPress());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, pst, null);
		}
		return m;
	}

	public int deleteBook(Integer id) {
		String sql = "delete book where id=?";
		Connection con=null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			con=DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, id);
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, pst, null);
		}
		return m;
	}

	public int updateBook(Book b) {
		String sql = "update book set name=?,publish_date=?,author=?,price=?,is_new=?,press=?  where id=?";
		Connection con=null;
		PreparedStatement pst = null;
		int m = 0;
		try {
			con=DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, b.getName());
			pst.setDate(2, new java.sql.Date(b.getPublishDate().getTime()));
			pst.setString(3,b.getAuthor());
			pst.setDouble(4, b.getPrice());
			pst.setBoolean(5, b.isNew());
			pst.setString(6, b.getPress());
			pst.setInt(7, b.getId());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(null, pst, null);
		}
		return m;
	}

	public Book loadBook(Integer id) {
		Book b=null;
		
		String sql = "select * from book where id=?";
		Connection con=null;
		PreparedStatement pst = null;
		ResultSet rs =null;
		try {
			con=DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setInt(1, id);;
			rs = pst.executeQuery();
			if (rs.next()) {
				b=new Book();
				String name = rs.getString("name");
				Date publishDate = new Date(rs.getDate("publish_date").getTime());
				String author = rs.getString("author");
				double price = rs.getDouble("price");
				boolean isNew = rs.getBoolean("is_new");
				String press = rs.getString("press");
				b.setId(id);
				b.setName(name);
				b.setPublishDate(publishDate);
				b.setAuthor(author);
				b.setPrice(price);
				b.setNew(isNew);
				b.setPress(press);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(null, pst, rs);
		}
		return b;
	}

	public List<Book> findAll() {
		List<Book> list=new ArrayList<Book>();
		String sql = "select * from book";
		Connection con=null;
		PreparedStatement pst = null;
		ResultSet rs =null;
		try {
			con=DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			
			rs = pst.executeQuery();
			if (rs.next()) {
				Book b=new Book();
				int id=rs.getInt("id");
				String name = rs.getString("name");
				Date publishDate = new Date(rs.getDate("publish_date").getTime());
				String author = rs.getString("author");
				double price = rs.getDouble("price");
				boolean isNew = rs.getBoolean("is_new");
				String press = rs.getString("press");
				b.setId(id);
				b.setName(name);
				b.setPublishDate(publishDate);
				b.setAuthor(author);
				b.setPrice(price);
				b.setNew(isNew);
				b.setPress(press);
				list.add(b);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(null, pst, rs);
		}
		return list;
	}

	public int insertAll() {
		Connection con=null;
		String sql = "insert into book(id,name,publish_date,author,price,is_new,press) values(seq03.nextval,?,?,?,?,?,?)";
		PreparedStatement pst = null;
		final int batchSize = 1000;
		con=DBUtil.createConnection();
		int count = 1;
		int times = 0;
		try{
			pst = con.prepareStatement(sql);
			ArrayList<Book> books = FileProcessDataUtil.writeAndReadData();
			System.out.println(books.size());
			for(Book b:books){
				pst.setString(1, b.getName());
				pst.setDate(2, new java.sql.Date(b.getPublishDate().getTime()));
				pst.setString(3, b.getAuthor());
				pst.setDouble(4, b.getPrice());
				pst.setBoolean(5, b.isNew());
				pst.setString(6, b.getPress());
				pst.addBatch();
				if(++count % batchSize == 0 ){
			        pst.executeBatch();
			        count = 0;
			    }
				times++;
			}
			pst.executeBatch();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBUtil.close(null, pst, null);
		}
		return times;
		
	}

}
